/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.DaftarKelasDAO;
import com.ivanbiz.dao.KelasDAO;
import com.ivanbiz.dao.impl.DaftarKelasDAOImpl;
import com.ivanbiz.dao.impl.KelasDAOImpl;
import com.ivanbiz.model.DaftarKelas;
import com.ivanbiz.model.Kelas;
import com.ivanbiz.model.Pengajar;
import com.ivanbiz.model.Pengguna;
import com.ivanbiz.model.Perusahaan;
import com.ivanbiz.report.DaftarKelasReport;
import com.ivanbiz.service.Email;
import com.ivanbiz.service.GlobalSession;
import com.ivanbiz.service.ServiceHelper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author Shbt Peterpan
 */
public class DaftarKelasReportDialog extends javax.swing.JDialog {

    DaftarKelasDAO daftarKelasDAO;
    List<DaftarKelas> listDaftarKelas;
    List<Kelas> listKelas;
    KelasDAO kelasDAO;
    SimpleDateFormat sdf;
    String lulus;
    Date dari;
    Date sampai;
    Pengajar pengajar;

    public DaftarKelasReportDialog(String kelulusan) {
        initComponents();
        daftarKelasDAO = new DaftarKelasDAOImpl();
        kelasDAO = new KelasDAOImpl();
        sdf = new SimpleDateFormat("dd-MMMM-yyyy");
        this.lulus = kelulusan;
        jLabelJudul.setVisible(false);
        buttonKirimEmailAll.setVisible(false);
        refresh();
    }

    public DaftarKelasReportDialog(Pengajar pengajar, Date dari, Date sampai) {
        try {
            initComponents();
            this.dari = dari;
            this.sampai = sampai;
            daftarKelasDAO = new DaftarKelasDAOImpl();
            kelasDAO = new KelasDAOImpl();
            sdf = new SimpleDateFormat("dd-MMMM-yyyy");
            jLabelJudul.setText("Dari tanggal " + sdf.format(dari) + " sampai tanggal " + sdf.format(sampai));
            if (pengajar.getNama().equals("ALL")) {
                List<Pengguna> listGroups = GlobalSession.getListGroups();
                String email = "";
                String nama = "";
                for (Pengguna pengguna : listGroups) {
                    if (pengguna.getEmail() != null) {
                        email += pengguna.getEmail() + ",";
                        nama += pengguna.getUserName() + ",";
                    }
                }
                pengajar.setNama(nama);
                pengajar.setEmail(email);
                this.pengajar = pengajar;
                listKelas = kelasDAO.getData(dari, sampai);
            } else {
                this.pengajar = pengajar;
                listKelas = kelasDAO.getData(pengajar, dari, sampai);
            }
            updateTableDaftarKelas();
        } catch (Exception ex) {
            Logger.getLogger(DaftarKelasReportDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableKelas = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        buttonPreview = new javax.swing.JButton();
        buttonCetak = new javax.swing.JButton();
        buttonKirimEmail = new javax.swing.JButton();
        buttonKirimEmailAll = new javax.swing.JButton();
        jLabelJudul = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setModal(true);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Laporan Daftar Kelas");

        tableKelas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableKelas);

        buttonPreview.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/preview.jpg"))); // NOI18N
        buttonPreview.setText("Lihat Daftar Kelas Terseleksi");
        buttonPreview.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPreviewActionPerformed(evt);
            }
        });
        jPanel1.add(buttonPreview);

        buttonCetak.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/cetak.jpg"))); // NOI18N
        buttonCetak.setText("Cetak Daftar Kelas Terseleksi");
        buttonCetak.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCetakActionPerformed(evt);
            }
        });
        jPanel1.add(buttonCetak);

        buttonKirimEmail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/transfer.jpg"))); // NOI18N
        buttonKirimEmail.setText("Kirim Email Kelas Terseleksi");
        buttonKirimEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonKirimEmailActionPerformed(evt);
            }
        });
        jPanel1.add(buttonKirimEmail);

        buttonKirimEmailAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/transfer.jpg"))); // NOI18N
        buttonKirimEmailAll.setText("Kirim Email Kelas");
        buttonKirimEmailAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonKirimEmailAllActionPerformed(evt);
            }
        });
        jPanel1.add(buttonKirimEmailAll);

        jLabelJudul.setText("Dari Tanggal {} Sampai {}");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 830, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 830, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 830, Short.MAX_VALUE)
                    .addComponent(jLabelJudul, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelJudul)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-866)/2, (screenSize.height-638)/2, 866, 638);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonPreviewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPreviewActionPerformed
        if (tableKelas.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dilihat", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                listDaftarKelas = daftarKelasDAO.getDataByEqualsOrderByBankAndNama(listKelas.get(tableKelas.getSelectedRow()).getTransactionReference());
                if (listDaftarKelas.isEmpty()) {
                    JOptionPane.showMessageDialog(this, "Tidak ada Murid dalam Kelas " + listKelas.get(tableKelas.getSelectedRow()).getTransactionReference(), "warning", JOptionPane.WARNING_MESSAGE);
                } else if ("kelulusan".equals(lulus)) {
                    new DaftarKelasReport().previewAndCetakDaftarKelas(listDaftarKelas, "preview", "kelulusan");
                } else {
                    new DaftarKelasReport().previewAndCetakDaftarKelas(listDaftarKelas, "preview", "daftarKelas");
                }
            } catch (Exception ex) {
                Logger.getLogger(DaftarKelasReportDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonPreviewActionPerformed

    private void buttonCetakActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCetakActionPerformed
        if (tableKelas.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dicetak", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                listDaftarKelas = daftarKelasDAO.getDataByEqualsOrderByBankAndNama(listKelas.get(tableKelas.getSelectedRow()).getTransactionReference());
                if (lulus.contentEquals("kelulusan")) {
                    new DaftarKelasReport().previewAndCetakDaftarKelas(listDaftarKelas, "cetak", "kelulusan");
                } else {
                    new DaftarKelasReport().previewAndCetakDaftarKelas(listDaftarKelas, "cetak", "daftarKelas");
                }
            } catch (Exception ex) {
                Logger.getLogger(DaftarKelasReportDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonCetakActionPerformed

    private void buttonKirimEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonKirimEmailActionPerformed
        if (tableKelas.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Pilih data yang akan dikirim email", "warning", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                Perusahaan perusahaan = GlobalSession.getPerusahaan();
                listDaftarKelas = daftarKelasDAO.getDataByEqualsOrderByBankAndNama(listKelas.get(tableKelas.getSelectedRow()).getTransactionReference());
                DaftarKelasReport daftarKelasReport = new DaftarKelasReport();
                daftarKelasReport.previewAndCetakDaftarKelas(listDaftarKelas, "email", "daftarKelas");

                Kelas kelas = listKelas.get(tableKelas.getSelectedRow());
                File file = daftarKelasReport.getReportFile();
//            File file = new File(System.getProperty("user.dir") + "\\report\\" + kelas.getTransactionReference() + ".pdf");
                if (!file.exists()) {
                    JOptionPane.showMessageDialog(null, "file gagal dibuat");
                } else if (perusahaan.getEmail().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "Email Perusahaan kosong");
                } else if (kelas.getPengajar().getEmail().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "Email Pengajar kosong");
                } else {
                    Email email = new Email();
                    String password = perusahaan.getPassEmail();
                    String from = perusahaan.getEmail();
                    String to = kelas.getPengajar().getEmail();
                    String subject = "Jadwal Kelas " + kelas.getTransactionReference() + " Tanggal " + kelas.getTanggalKelas();
                    String message =
                            "Kepada Yth " + kelas.getPengajar().getNama() + "\n\n"
                            + "Detail Kelas anda sebagai berikut : \n"
                            + "No Kelas  = " + kelas.getTransactionReference() + "\n"
                            + "Deskripsi =" + kelas.getDeskripsi() + "\n"
                            + "Tanggal   =" + sdf.format(kelas.getTanggalKelas()) + "\n"
                            + (null == kelas.getTanggalKelas2() ? "" : "Tanggal 2 =" + kelas.getTanggalKelas2()) + "\n"
                            + "Tempat    =" + kelas.getTempatKelas() + "\n"
                            + "Alamat    =" + kelas.getAlamatKelas() + "\n\n"
                            + "Terimakasih\n\n"
                            + perusahaan.getNama();

                    String[] attachments = {file.getAbsolutePath()};

                    email.sendMail(from, password, from, to, subject, message, attachments);
                    file.delete();

                    JOptionPane.showMessageDialog(rootPane, "Jadwal Kelas sudah dikirim");
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(rootPane, ex.getMessage());
                Logger.getLogger(DaftarKelasReportDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonKirimEmailActionPerformed

    private void buttonKirimEmailAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonKirimEmailAllActionPerformed
        Perusahaan perusahaan = GlobalSession.getPerusahaan();
        sdf = new SimpleDateFormat("dd-MMM-yy");
        DaftarKelasReport daftarKelasReport = new DaftarKelasReport();
        daftarKelasReport.previewKelas(listKelas, jLabelJudul.getText());
        File file = daftarKelasReport.getReportFile();
//        File file = new File(System.getProperty("user.dir") + "\\report\\Kelas_" + sdf.format(new Date()) + ".pdf");
        if (!file.exists()) {
            JOptionPane.showMessageDialog(null, "file gagal dibuat");
        } else if (perusahaan.getEmail().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Email Perusahaan kosong");
        } else if (pengajar.getEmail().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Email Pengajar kosong");
        } else {
            try {
                Email email = new Email();
                String password = perusahaan.getPassEmail();
                String from = perusahaan.getEmail();
                String to = pengajar.getEmail();
                String subject = "Daftar Kelas " + jLabelJudul.getText();
                String message = "Kepada Yth " + pengajar.getNama() + "\n\n" + "Detail Kelas " + jLabelJudul.getText() + " terlampir. \n\n\n" + "Terimakasih\n\n" + perusahaan.getNama();
                String[] attachments = {file.getAbsolutePath()};
                email.sendMail(from, password, from, to, subject, message, attachments);
//                file.delete();
                JOptionPane.showMessageDialog(rootPane, "Jadwal sudah dikirim");
            } catch (MessagingException ex) {
                Logger.getLogger(DaftarKelasReportDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_buttonKirimEmailAllActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCetak;
    private javax.swing.JButton buttonKirimEmail;
    private javax.swing.JButton buttonKirimEmailAll;
    private javax.swing.JButton buttonPreview;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelJudul;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableKelas;
    // End of variables declaration//GEN-END:variables

    private void refresh() {
        try {
            listKelas = kelasDAO.getAll(Kelas.class);
            updateTableDaftarKelas();
        } catch (Exception ex) {
            Logger.getLogger(DaftarKelasReportDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTableDaftarKelas() {
        String[] judul = {"No", "No Kelas", "Deskripsi", "Pengajar", "Tanggal", "Tanggal 2", "Tempat", "Alamat"};
        Object[][] isi = new Object[listKelas.size()][8];
        int x = 0;
        int no = 0;
        for (Kelas kelass : listKelas) {
            no += 1;
            isi[x][0] = no;
            isi[x][1] = kelass.getTransactionReference();
            isi[x][2] = kelass.getDeskripsi();
            isi[x][3] = "(" + kelass.getPengajar().getNIP() + ") - " + kelass.getPengajar().getNama();
            isi[x][4] = sdf.format(kelass.getTanggalKelas());
            isi[x][5] = kelass.getTanggalKelas2();
            isi[x][6] = kelass.getTempatKelas();
            isi[x][7] = kelass.getAlamatKelas();
            x++;
        }
        tableKelas.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        new ServiceHelper().setAutoRize(isi, judul, tableKelas);
    }

    private void update() {
        BufferedWriter bfw = null;
        try {
            bfw = new BufferedWriter(new FileWriter("D:\\Data.txt"));
            for (int i = 0; i < tableKelas.getColumnCount(); i++) {
                //first loop is used for titles of each column
                String name = String.valueOf(tableKelas.getColumnName(i));
                if (name.length() > 20) {
                    //20 (characters long) is the constant I chose to make each value
                    name = name.substring(0, 20);
                } else if (name.length() == 20) {
                } else {
                    String spaces = "";
                    int diff = 20 - name.length();
                    while (diff > 0) {
                        spaces = spaces + " ";
                        diff--;
                    }
                    name = name.concat(spaces);
                }
                bfw.write(name);
                bfw.write("\t");
            }
            for (int i = 0; i < tableKelas.getRowCount(); i++) {
                //for all the data in the Jtable excluding column headers
                bfw.newLine();
                for (int j = 0; j < tableKelas.getColumnCount(); j++) {
                    if (tableKelas.getValueAt(i, j) == null) {
                        bfw.write("                    ");
                        bfw.write("\t");
                    } else {
                        String name = String.valueOf(tableKelas.getValueAt(i, j));
                        if (name.contains("(")) {
                            name = name.substring(0, name.indexOf("("));
                        }
                        if (name.length() > 20) {
                            name = name.substring(0, 20);
                        } else if (name.length() == 20) {
                        } else {
                            String spaces = "";
                            int diff = 20 - name.length();
                            while (diff > 0) {
                                spaces = spaces + " ";
                                diff--;
                            }
                            name = name.concat(spaces);
                        }
                        bfw.write(name);
                        bfw.write("\t");
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(DaftarKelasReportDialog.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                bfw.close();
            } catch (IOException ex) {
                Logger.getLogger(DaftarKelasReportDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
